package com.bbx.taxi.client.Util;

import android.content.Context;
import android.graphics.Point;
import android.os.AsyncTask;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.yanxuwen.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：严旭文 on 2017/1/9 11:53
 * 邮箱：420255048@qq.com
 */
public class ZoomUtils {
    public static  void setZoom(final Context context,final BaiduMap mBaiduMap, final int mapwidth_center,final int mapheight_center,final int width, final int height, final LatLng lla, final LatLng llb){
        new AsyncTask<Void, Void, Void>(){
            LatLngBounds latlngBounds;
            @Override
            protected Void doInBackground(Void... voids) {
                List<LatLng> points = new ArrayList<LatLng>();
                points.add(lla);
                points.add(llb);
                LatLngBounds.Builder builder2 = new LatLngBounds.Builder();
                for (LatLng p : points) {
                    builder2 = builder2.include(p);
                }
                 latlngBounds = builder2.build();
                return null;
            }
            @Override
            protected void onPostExecute(Void aVoid) {
                //先设置中心点
                LatLng centll = new LatLng((lla.latitude + llb.latitude) / 2,
                        (lla.longitude + llb.longitude) / 2);
                MapStatus.Builder builder = new MapStatus.Builder();
                Point p = new Point(mapwidth_center, mapheight_center);
                builder.target(centll).targetScreen(p);
                final MapStatus mMapStatus = builder.build();
                MapStatusUpdate u = MapStatusUpdateFactory
                        .newMapStatus(mMapStatus);
                mBaiduMap.animateMapStatus(u);
                int paddin= DensityUtil.dip2px(context,13);
                u = MapStatusUpdateFactory.newLatLngBounds(latlngBounds, width - paddin, height-paddin);
                mBaiduMap.animateMapStatus(u);
                super.onPostExecute(aVoid);
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }
}
